﻿using System.Collections.Generic;
using QFramework;
using UnityEngine;

namespace ProjectMyFarmAndShe
{
    public interface IGridSystem : ISystem
    {
        DynaGrid<GridData> Grids { get; set; }
    }
    
    public class GridSystem : AbstractSystem,IGridSystem
    {
        public DynaGrid<GridData> Grids { get; set; }
        
        protected override void OnInit()
        {
            Grids = new DynaGrid<GridData>();

            var saveUtility = this.GetUtility<ISaveUtility>();
            
            saveUtility.AddSaveDataAction(utility =>
            {
                var saveGridData = new SaveGridData();
                
                Grids.ForEach((x, y, data) =>
                {
                    var saveGridConfig = new SaveGridConfig();
                    saveGridConfig.X = x;
                    saveGridConfig.Y = y;
                    if (data.PlantItem != null)
                    {
                        saveGridConfig.PlantItemKey = data.PlantItem.Key;
                        saveGridConfig.PlantIndex = data.PlantItem.PlantIndex;
                        saveGridConfig.CurrentState = data.PlantItem.CurrentPlantState;
                    }
                    saveGridConfig.IsWater = data.mIsWater;
                    
                    
                    saveGridData.Datas.Add(saveGridConfig);
                });
                
                saveUtility.SaveString("GridDatas",JsonUtility.ToJson(saveGridData));
            });

        }
    }
}